package com.wangyang.stock.business.services;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.BeanUtils;

import com.wangyang.stock.business.IEmployeeService;
import com.wangyang.stock.dao.IDao;
import com.wangyang.stock.po.Employee;
import com.wangyang.stock.po.Employment;
import com.wangyang.stock.po.Operator;
import com.wangyang.stock.vo.EmployeeVo;

public class EmployeeServiceImp implements IEmployeeService {

	private IDao dao;

	public IDao getDao() {
		return dao;
	}

	public void setDao(IDao dao) {
		this.dao = dao;
	}

	public List selectEmployee(EmployeeVo employeeVo) {
		// TODO Auto-generated method stub

		StringBuffer hql = new StringBuffer("from Employee emp where 1=1 ");
		if (null != employeeVo) {
			if (null != employeeVo.getEmployeeId()
					&& 0 != employeeVo.getEmployeeId()) {
				hql.append(" and emp.employeeId = '");
				hql.append(employeeVo.getEmployeeId());
				hql.append("'");
			}
			if (null != employeeVo.getEmployeeName()) {
				hql.append(" and emp.employeeName like '%");
				hql.append(employeeVo.getEmployeeName());
				hql.append("%'");
			}
			if (null != employeeVo.getEmpEmplId()) {
				hql.append(" and emp.employment.employmentId = '");
				hql.append(employeeVo.getEmpEmplId());
				hql.append("'");
			}
			if (null != employeeVo.getEmployeeTel()) {
				hql.append(" and emp.employeeTel like '%");
				hql.append(employeeVo.getEmployeeTel());
				hql.append("%'");
			}
			if (null != employeeVo.getEmployeeAdd()) {
				hql.append(" and emp.employeeAdd like '%");
				hql.append(employeeVo.getEmployeeAdd());
				hql.append("%'");
			}
			if (null != employeeVo.getEmployeePermission()) {
				hql.append(" and emp.employeePermission like '%");
				hql.append(employeeVo.getEmployeePermission());
				hql.append("%'");
			}
		}
		List<Employee> list = dao.select(hql.toString());

		List<EmployeeVo> volist = new ArrayList();
		for (int i = 0; i < list.size(); i++) {
			Employee emp = list.get(i);
			EmployeeVo empVo = new EmployeeVo();
			BeanUtils.copyProperties(emp, empVo);
			empVo.setEmpEmplId(emp.getEmployment().getEmploymentId());
			empVo.setEmpEmplName(emp.getEmployment().getEmploymentName());
			volist.add(empVo);
		}
		return volist;
	}

	public boolean createEmployee(EmployeeVo employeeVo) {
		// TODO Auto-generated method stub

		Employee employee = new Employee();

		employee.setEmployeeAdd(employeeVo.getEmployeeAdd());
		employee.setEmployeeName(employeeVo.getEmployeeName());
		employee.setEmployeeMemo(employeeVo.getEmployeeMemo());
		employee.setEmployeePermission(employeeVo.getEmployeePermission());
		employee.setEmployeeTel(employeeVo.getEmployeeTel());
		employee.setEmployment(new Employment(employeeVo.getEmpEmplId()));
		employee.setIsOper(employeeVo.getIsOper());
		boolean is = false;
		if (dao.create(employee)) {
			if (employee.getIsOper() == 1) {
				Operator operator = new Operator();
				operator.setOperatorId(employee.getEmployeeId());
				operator.setEmployee(employee);
				operator.setOperatorName("" + employee.getEmployeeId());
				operator.setOperatorPass("0000");
				dao.create(operator);
			}
			is = true;
		}
		return is;
	}

	public boolean updateEmployee(EmployeeVo employeeVo) {
		// TODO Auto-generated method stub
		Employee employee = new Employee();
		BeanUtils.copyProperties(employeeVo, employee);
		employee.setEmployment(new Employment(employeeVo.getEmpEmplId()));
		//
		Employee empTemp = new Employee(employee.getEmployeeId());
		StringBuffer hql = new StringBuffer("from Employee emp where 1=1 ");
		hql.append(" and emp.employeeId = '");
		hql.append(empTemp.getEmployeeId());
		hql.append("'");

		Employee emp = (Employee) dao.select(hql.toString()).get(0);
		if (employeeVo.getIsOper() != emp.getIsOper()
				&& employeeVo.getIsOper() == 0) {
			Operator operator = new Operator();
			operator.setOperatorId(emp.getEmployeeId());
			dao.delete(operator);
		}
		if (employeeVo.getIsOper() != emp.getIsOper()
				&& employeeVo.getIsOper() == 1) {
			Operator operator = new Operator();
			operator.setOperatorId(emp.getEmployeeId());
			operator.setOperatorName("" + employee.getEmployeeId());
			operator.setOperatorPass("0000");
			operator.setEmployee(emp);
			dao.create(operator);
		}
		return dao.update(employee);
	}

	public boolean deleteEmployee(EmployeeVo employeeVo) {
		// TODO Auto-generated method stub
		Employee employee = new Employee();
		BeanUtils.copyProperties(employeeVo, employee);
		if (employeeVo.getIsOper() == 1) {
			Operator operator = new Operator();
			operator.setOperatorId(employee.getEmployeeId());
			dao.delete(operator);
		}
		return dao.delete(employee);
	}
}
